import Common from "../Common";
import TaskItem from "./TaskItem";


const {ccclass, property} = cc._decorator;

/**
 * 天天抽奖
 */
@ccclass
export default class DrawDialog extends Common {

    @property({
        type: cc.Node,
        displayName: '内容区域容器'
    })
    content: cc.Node

    @property(cc.Label)
    descLabel: cc.Label

    protected taskItem: cc.Prefab

    protected start() {
        this.init();
    }

    protected watch = {
        userInfo: (val) => {
            this.descLabel.string = val.draw_desc || ''
        }
    }

    async init() {
        const taskItemPrefab = await this.getItem();
        await this.content.removeAllChildren(true)
        const userInfo = await this.initUserInfo()
        if (!userInfo) {
            return;
        }
        const arr = [
            {id: 1, key: 'sign', name: '前往抽奖', desc: `抽奖需要消耗${userInfo.draw_ticket_price || 1}金币`},
            {id: 2, key: 'sign', btnStr: '查看', name: '抽奖历史', desc: '查看历史的抽奖记录'},
            {id: 3, key: 'sign', btnStr: '查看', name: '往期中奖', desc: '查看往期的中奖用户记录'},
        ];
        arr.forEach(item => {
            const goodItem = cc.instantiate(taskItemPrefab)
            goodItem.getComponent(TaskItem).init({
                ...item,
                target: this,
                onClick: this.onClick,
            });
            this.content.addChild(goodItem)
        })
    }

    async onClick(id, key) {
        this.onBtnClick()
        switch (id) {
            case 1: // 前往抽奖
                cc.director.loadScene('drawPage')
                break;
            case 2: // 抽奖记录
                cc.director.loadScene('drawRecords')
                break;
            case 3: // 用户中奖
                cc.director.loadScene('winningRecords')
                break;
        }
        this.node.active = false;
    }


    protected async getItem() {
        if (!this.taskItem) {
            this.taskItem = await this.getPrefab('taskItem')
        }
        return this.taskItem
    }


}
